package freelinz.smart.gwtex.client.ui;


import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import freelinz.smart.gwtex.client.ui.SmartKitsAll.Resized;
import freelinz.smart.gwtex.user.client.DOMEx;

public class SmartKit extends AbstractSmartKit
{
    protected SimplePanel widget;
    protected Resized resizedWidget;
    
    public SmartKit()
    {
        this(new SimplePanel());
    }
    
    public SmartKit(SimplePanel widget)
    {
        super();
        this.widget = widget;
        this.widget.setStyleName("SmartKit");
    }

    public Widget getWidget()
    {
        return this.widget;
    }

    public void addResizedWidget(Resized w)
    {
        if(w == null || w.getWidget() == null || w == this)
        {
            throw new IllegalArgumentException("w == null || w.getWidget() == null || w == this");
        }
        clear();
        resizedWidget = w;
        this.widget.add(w.getWidget());
    }

    public void resized(int width, int height)
    {
        Logger.debug(SmartKit.class.toString()+" width:"+width);
        Logger.debug(SmartKit.class.toString()+" heigth"+height);
        Element elem = widget.getElement();
        DOMEx.setOffsetMarginSize(elem, width, height);
        
        fireResized(width-DOMEx.getPaddingLeftRight(elem)
                -DOMEx.getBorderLeftRight(elem)-DOMEx.getPaddingLeftRight(elem), 
                height-DOMEx.getPaddingTopBottom(elem)
                -DOMEx.getBorderLeftRight(elem)-DOMEx.getPaddingLeftRight(elem));
    }
    public void setSizeScale(Float width,Float height)
    {
        if((width!=null && width.floatValue() > 1) ||(height!=null && height.floatValue()>1))
        {
            throw new IllegalArgumentException("setSizeScale(Float width,Float height)");
        }
        if(width != null )
        {
            widget.setWidth((int)(width.floatValue()*100)+"%");
        }
        if(height != null)
        {
            widget.setHeight((int)(height.floatValue()*100)+"%");
        }
        DeferredCommand.add(new Command(){
            public void execute()
            {
                Element elem = widget.getElement();
                int w = widget.getOffsetWidth()
                    -DOMEx.getPaddingLeftRight(elem)
                    -DOMEx.getBorderLeftRight(elem);
                int h = widget.getOffsetHeight()
                    -DOMEx.getBorderTopBottom(elem)
                    -DOMEx.getPaddingTopBottom(elem);
                fireResized(w, h);
            }
        });
    }

    public void removeResizedWidget(Resized w)
    {
        if(w == null)
        {
            w = resizedWidget;
            if(w == null)return;
        }
        widget.remove(w.getWidget());
    }

    public Resized getResizedWidget()
    {
        return resizedWidget;
    }
    public static class ResizedWidget implements Resized
    {
        private Widget widget;
        public ResizedWidget(Widget w)
        {
            this.widget = w;
        }

        public Widget getWidget()
        {
            return this.widget;
        }

        public void resized(int width, int height)
        {
            DOMEx.setOffsetMarginSize(widget.getElement(), width, height);
        }
    }
    public void clear()
    {
        super.clear();
        if(widget != null)widget.clear();
        resizedWidget = null;
        setParentSmartKits(null);
    }
}
